<template>
  <div class="picture-list">
    <!-- 图片列表 -->
    <a-list
      :grid="{ gutter: 16, xs: 1, sm: 2, md: 3, lg: 4, xl: 4, xxl: 6 }"
      :data-source="dataList"
      :loading="loading"
    >
      <template #renderItem="{ item: picture }">
        <a-list-item style="padding: 0">
          <!-- 单张图片 -->
          <a-card hoverable @click="doClickPicture(picture)">
            <template #cover>
              <img
                style="height: 180px; object-fit: cover"
                :alt="picture.name"
                :src="picture.thumbnailUrl ?? picture.url"
                loading="lazy"
              />
            </template>
            <a-card-meta :title="picture.name">
              <template #description>
                <a-flex>
                  <a-tag color="green">
                    {{ picture.category ?? '默认' }}
                  </a-tag>
                  <a-tag v-for="tag in picture.tags" :key="tag">
                    {{ tag }}
                  </a-tag>
                </a-flex>
              </template>
            </a-card-meta>
            <template #actions v-if="showOp">
              <a-space @click="(e) => doShare(picture, e)">
                <ShareAltOutlined />
                分享
              </a-space>
              <a-space @click="(e) => doEdit(picture, e)">
                <edit-outlined />
                编辑
              </a-space>
              <a-space @click="(e) => doDelete(picture, e)">
                <delete-outlined />
                删除
              </a-space>
            </template>
          </a-card>
        </a-list-item>
      </template>
    </a-list>
  </div>
  <!-- 分享弹窗 -->
  <ShareModal ref="ShareModalRef" :link="shareLink" :title="shareTitle" />

</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
import { deletePictureUsingPost } from '@/api/pictureController.ts'
import { message } from 'ant-design-vue'
import { ShareAltOutlined, EditOutlined, DeleteOutlined } from '@ant-design/icons-vue';
import ShareModal from '@/components/ShareModal.vue'
import { ref } from 'vue'


interface Props {
  dataList?: API.PictureVO[]
  loading?: boolean
  spaceId?: number
  showOp?: boolean
  refresh?: () => void
}

const props = withDefaults(defineProps<Props>(), {
  dataList: () => [],
  loading: false,
  showOp: false,
})

// 跳转至图片详情
const router = useRouter()
const doClickPicture = (picture) => {
  router.push({
    path: `/picture/${picture.id}`,
    query: {
      spaceId: props.spaceId
    }
  })
}

// 分享
const shareLink = ref<string>();
const shareTitle = ref();
const ShareModalRef = ref()
const doShare = (picture: API.PictureVO, e: Event) => {
  // 防止冒泡
  e.stopPropagation()
  shareTitle.value = "分享图片"
  shareLink.value = `${window.location.protocol}//${window.location.host}/picture/${picture.id}/${true}`
  ShareModalRef.value.openModal()
}

// 编辑
const doEdit = (picture: API.PictureVO, e: Event) => {
  e.stopPropagation()
  router.push({
    path: '/add_picture',
    query: {
      id: picture.id,
      spaceId: picture.spaceId,
    },
  })
}

// 删除
const doDelete = async (picture: API.PictureVO, e: Event) => {
  e.stopPropagation()
  const id = picture.id
  if (!id) {
    return
  }
  const res = await deletePictureUsingPost({ id })
  if (res.data.code === 0) {
    message.success('删除成功')
    // 让外层刷新
    props?.refresh()
  } else {
    message.error('删除失败')
  }
}


</script>

<style scoped>

.picture-list :deep(.ant-card-body) {
  border: none;
  padding: 15px;
  /*text-align: center;*/
}

.picture-list :deep(.ant-card-meta-title) {
  font-weight: 600;
  margin-bottom: 0;
}

.picture-list :deep(.ant-card-bordered) {
  border: none;
  /*background-color: #f8f8f8;*/
}
</style>
